package jdbc.homework01;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

//4:用 PreparedStatement 实现删除功能（表随意）
public class Delete {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		Connection con = null;
		con = DbUtil.getConnection();
		Statement st = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = null;
		System.out.println("请输入学号：");
		int stuno = in.nextInt();
		System.out.println("请输入分数：");
		float score = in.nextFloat();
		// 先查询该学生可在考试表里
		sql = "select * from s_examresult where st_id =? and score =?";
		try {
			ps = con.prepareStatement(sql);
			ps.setInt(1, stuno);
			ps.setFloat(2, score);
			rs = ps.executeQuery();
			if (rs.next()) {
				sql = "delete from s_examresult where st_id =? and score =?";
				ps = con.prepareStatement(sql);
				ps.setInt(1, stuno);
				ps.setFloat(2, score);
				int a = ps.executeUpdate();
				System.out.println("删除" + a + "信息");
			} else {
				System.out.println("学生不存在或者没有改成绩的学生，删除失败");
			}
		} catch (SQLException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}

	}

}
